/**----------------------------------------------------------*/
/** JS:  cmi_ad_list.js（单页管理--单页内容）           */
/**----------------------------------------------------------*/

var tipc = 'AD';
var tipcType = 'AD_TYPE';
var tipcLoc = 'AD_LOC';
var win = null;

var add = function() {
	try {
		var div = getEditDiv(null);
		
		win = MyUtils._getToolbarWin("dataPanel", "editform", div, submitAdd, panel + "--添加","保存提交");
		win.window({
			width:700,
			height:500,
			modal: true
		});
		initDate();
		loadObjectList("typeId",tipcType);
		loadObjectList("adLocId",tipcLoc);
		repainAd(null);
	} catch (e) {
		showJsException(e);
	}
};

var modify = function() {
	try{
		var record = datagridOneRow('dataPanel');
		
		if(record == null) {
			return;
		}
		var div = getEditDiv(record);
		win = MyUtils._getToolbarWin("dataPanel", "editform", div, submitModify, panel +"--修改","保存提交");
		win.window({
			width:700,
			height:500,
			modal: true
		});
		initDate();
		loadObjectList("typeId",tipcType);
		loadObjectList("adLocId",tipcLoc);
		repainAd(record);
	} catch(e) {
		showJsException(e);
	}
};

var remove = function() {
	try{
		var rows = $('#dataPanel').datagrid("getSelections");
		if(rows.length == 0) {
			MyUtils.waring(_empty_select);
			return;
		}
		var ids = new Array();
		var names = new Array();
		for(var i = 0; i < rows.length; i++) {
			ids.push(rows[i].uuid);
			names.push(rows[i].brandName);
		}
		var data = { data:"["+MyUtils.createQueryConditionBean("uuid","customer","uuid IN (" + ids.toString() + ")","0",null) +"]"
			    ,st:tipc
			    ,msg:names.toString()
		};
		$.messager.confirm(_msg_sys,MyUtils.valueAlign(_remove_confirm,"left"), function(r) {
			if (r) {
				MyUtils.openMask(_msg_process,$('#t2'));
				MyUtils.sendPost("impl_remove/backRemove.adm",data,function(rets){
					try{
						MyUtils.hiddenMask($('#t2'));
						var res = MyUtils.toJsonObject(rets);
						if(res.error == '0') {
							MyUtils.info(res.msg);
							$('#dataPanel').datagrid("clearSelections");
							$('#dataPanel').datagrid("reload");
						} else {
							MyUtils.error(res.msg);
						}
					} catch (e) {
						showJsException(e);
					}
				});
			}
		});
		
	} catch(e) {
		showJsException(e);
	}
};

var dbclick = function(index,row){
	$('#dataPanel').datagrid("clearSelections");
	$('#dataPanel').datagrid("selectRow",index);
	modify();
};

var initPanel = function() {
	var columns = [
	               
	               {field : 'name',title : '广告名称',width : 80,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"centr");}}
				    , {field : 'typeName',title : '媒介类型',width : 80,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"centr");}}
				    ,{field : 'locName',title : '广告位置',width : 80,sortable:true,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"center");}}
					,{field : 'startTime',title : '开始时间',width : 80,align : 'center',formatter:function(value){return MyUtils.valueAlign( value.substring(0,10) ,"center");}}
					,{field : 'endTime',title : '结束时间',width : 80,sortable:true,align : 'center',formatter:function(value){return MyUtils.valueAlign( value.substring(0,10) ,"center");}}
					,{field : 'clickSum',title : '点击次数',width : 50,sortable:true,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"center");}}
					,{field : 'enable',title : '是否启用',width : 50,sortable:true,align : 'center',formatter:function(value){return  value == '1' ? '是':'否';}}
					
					,{field : 'uuid',title : 'ID',hidden:true}
					,{field : 'adType',title : 'adType',hidden:true}
					,{field : 'adLocId',title : 'adLocId',hidden:true}
					,{field : 'link',title : '链接',hidden:true}
					,{field : 'adImg',title : '图片',hidden:true}
					,{field : 'imgLink',title : '外链',hidden:true}
					,{field : 'contractUser',title : 'contractUser',hidden:true}
					,{field : 'contractEmail',title : 'contractEmail',hidden:true}
					,{field : 'contractPhone',title : 'contractPhone',hidden:true}
					];
	var pam = {qr:"[]",st:tipc,sco:'',dc:Dmlx.sort.asc};
	MyUtils.createPaginPanelWithQueryparams('dataPanel',null,Dmlx._hFrozenColumns,columns,toolbars,dbclick, panel,null);
	$('#dataPanel').datagrid({
		url:"impl_search/commonsPaginSearch.ajax",
		queryParams:pam,
		pagination:true,
		fitColumns:true
	});
	$('#search').linkbutton({
		plain : true
	}).click(function(){
		var typeSelected = $('#typeSearch').val();
		var locSelected = $('#locSearch').val();
		var enableSelected = $('#enableSearch').val();
		var pam = {qr:"[]",st:tipc,sco:'',dc:Dmlx.sort.desc};
		
		var data = "";
		if(typeSelected !=  '0') {
			data = MyUtils.createQueryConditionBean("adType","=","?", typeSelected,null);
		}
		if(locSelected != '0' ){
			if(data != ""){
				data = data + ",";
			}
			data = data + MyUtils.createQueryConditionBean("adLocId","=","?", locSelected,null);

		}
		if(enableSelected != '0'){
			if(data != ""){
				data = data + ",";
			}
			data = data + MyUtils.createQueryConditionBean("enable","=","?", enableSelected == 1?1:0,null);

		}
		data = "[" + data + "]";
		
		pam = {qr:data,st:tipc,sco:'',dc:Dmlx.sort.desc};
		
		MyUtils._searchPanelData('dataPanel', 'impl_search/commonsPaginSearch.ajax', pam);
	});
};

$(function(){
	try {
		initPanel();
		loadObjectList("typeSearch",tipcType);
		loadObjectList("locSearch",tipcLoc);
	} catch (e) {
		showJsException(e,"数据加载失败!");
	}	
});

var submitAdd = function(){
	try {
		var dt = editValidator();
		if(dt != null) {
			MyUtils.openMask(_msg_process,win);
			$('#dt').val(dt);
			$('#dateEdit').attr("action",'impl_add/backFileUploadAdd.adm');
			$('#dateEdit').submit();
		}
	} catch (e) {
		MyUtils.hiddenMask(win);
		showJsException(e,_msg_fail);
	}
};

var submitModify = function(){
	try {
		var dt = editValidator();
		if(dt != null) {
			$.messager.confirm(_msg_sys,MyUtils.valueAlign(_modify_confirm,"left"), function(r) {
				if(r) {
					MyUtils.openMask(_msg_process,win);
					$('#dt').val(dt);
					$('#dateEdit').attr("action",'impl_modify/backFileUploadModify.adm?qy=');
					$('#dateEdit').submit();
					
				}
			});
		}
	} catch (e) {
		MyUtils.hiddenMask(win);
		showJsException(e,_msg_fail);
	}
};

var editValidator = function(){
	var record = {
			 uuid:$('#uuid').val()
			,name:$('#name').val()
			,adType:$('#typeId').val()
			,adLocId:$('#adLocId').val()
			,startTime:$("#startTime").datebox("getValue") 
			,endTime:$("#endTime").datebox("getValue") 
			,link:$('#link').val()
			,adImg:$('#adImg').val()
			,imgLink:$('#imgLink').val()
			,contractUser:$('#contractUser').val()
			,contractEmail:$('#contractEmail').val()
			,contractPhone:$('#contractPhone').val()
			
			,enable:$('input[name="enable"]:checked').val()
			,lastImg:$('#lastImg').val()
		};
	
	//验证名称
	if(MyUtils.isEmpty(record.name)) {
		MyUtils.waring("广告名称不能为空");
		return null;
	}
	//验证email
	if(!MyUtils.isEmpty(record.contractEmail) &&!MyUtils.isEmail(record.contractEmail)) {
		MyUtils.waring("email格式不正确，请确认");
		return null;
	}
	return MyUtils.toJsonStr(record);
};

//日期
var initDate = function(){
	MyUtils.createChineseDatebox('startTime');
	MyUtils.createChineseDatebox('endTime');
	var current = new Date();
	$('#startTime').datebox("setValue",MyUtils.getDate(current));
	$('#endTime').datebox("setValue",MyUtils.getDate(current));
}

var getEditDiv = function(record) {
	var div= '<div id="editform"><iframe name="ifrName" src="" id="ifrName" style="display:none;"></iframe><form target="ifrName" action="" method="POST" name="fmName" id="dateEdit" enctype="multipart/form-data">'
		+'<table cellspacing="2" cellpadding="2">'
		+'<tbody>'
		+'<tr>'
		+'<th align="right" width=200>广告名称：</th>'
		+'<td align="left" width=500>'
		+'<input type="hidden" id="uuid" value="" >'
		+'<input type="text" maxlength="120" id="name" style="width: 288px;" value=""><label class="requre_field">*</label>'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" width=200>媒介类型：</th>'
		+'<td align="left" width=500>'
		+'<select id="typeId" style="width: 208px;"></select>'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" width=200>广告位置：</th>'
		+'<td align="left" width=500>'
		+'<select id="adLocId" style="width: 208px;"></select>'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" valign="top" width=200>开始日期：</th>'
		+'<td align="left"> <input type="text"  id="startTime" style="width: 130px;" value=""></td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" valign="top" width=200>结束日期：</th>'
		+'<td align="left"> <input type="text"  id="endTime" style="width: 130px;" value=""></td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" width=200>广告链接：</th>'
		+'<td align="left" width=500>'
		+'<input type="text"  maxlength="5" id="link" style="width: 288px;" value="">'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" valign="top" width=200>上传广告图片：</th>'
		+'<td align="left" width=500>'
		+'<input type="file" name="file" id="adImg" style="width: 288px;" value="" size =40>'
		+'<div style="color:gray;">上传图片或指定远程URL为广告图片</div>	'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" width=200>或图片地址：</th>'
		+'<td align="left" width=500>'
		+'<input type="text"  maxlength="5" id="imgLink" style="width: 288px;" value="">'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" valign="top" width=200>是否开启：</th>'
		+'<td align="left">'
		+'<input type = "radio" name="enable"  value="1"	>是</input>'
		+'<input type = "radio" name="enable"  value="0"	>否</input></td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" width=200>广告联系人：</th>'
		+'<td align="left" width=500>'
		+'<input type="text"  maxlength="5" id="contractUser" style="width: 288px;" value="">'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" width=200>联系人email：</th>'
		+'<td align="left" width=500>'
		+'<input type="text"  maxlength="5" id="contractEmail" style="width: 288px;" value="">'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" width=200>联系电话：</th>'
		+'<td align="left" width=500>'
		+'<input type="text"  maxlength="5" id="contractPhone" style="width: 288px;" value="">'
		+'</td>'
		+'</tr>'
		+'</tbody>'
		+'</table>'
		+'<input type="hidden" id="lastImg" value="" >'
		+'<input type="hidden" id="dt" name="dt" value="" >'
		+'<input type="hidden" id="st" name="st" value="'+tipc+'" >'
		+'</form><div>';
	return div;
};


//分类
var loadObjectList = function(id, mytipc) {
	MyUtils.sendPost("impl_search/searchObjectList.ajax",{qy:"[]",st:mytipc},function(msg){
		try{
			var rest = MyUtils.toJsonObject(msg);
			if(rest.length > 0) {
				var opt='';
				for(var i = 0; i < rest.length; i++) {
					opt += '<option value="'+rest[i].uuid+'">'+rest[i].name+'</option>';
				}
				$('#' + id).append(opt);
			}
		} catch (e) {
		}
	});
};

//赋值
var repainAd = function(record){
	$('input[name="enable"][value='+(record ? record.enable : 1)+']').attr("checked",true);
	if(record) {
	
		$('#uuid').val(record.uuid);
		$('#name').val(record.name);
		$('#typeId').val(record.adType);
		$('#adLocId').val(record.adLocId);
		$('#startTime').val(record.startTime);
		$('#endTime').val(record.endTime);
		$('#link').val(record.link);
		$('#imgLink').val(record.imgLink);
		$('#contractUser').val(record.contractUser);
		$('#contractEmail').val(record.contractEmail);
		$('#contractPhone').val(record.contractPhone);
		$('#lastImg').val(record.adImg);

	}
	$('#ifrName').load(function() {
		try {
			MyUtils.hiddenMask(win);
			 var response = this.contentWindow.document.body.textContent;
			 if(!MyUtils.isEmpty(response)){
				var res = MyUtils.toJsonObject(response);
				if(res.error == '0') {
					MyUtils.info(res.msg);
					if(win)win.window('close');
					$('#dataPanel').datagrid("clearSelections");
					$('#dataPanel').datagrid("reload");
				} else {
					MyUtils.error(res.msg);
				}
			 }
		} catch (e) {
			MyUtils.hiddenMask(win);
			showJsException(e,_msg_fail);
		}
	});
};

/**
 * 获取数字
 */
var getNumber=function(val) {
	return isNaN(val) ? 0 :val;
};

